package Exercise;
//同步方法解决接口线程安全问题
public class windowTest2 {
    public static void main(String[] args) {
        MyThread1 myThread = new MyThread1();
        Thread t1 = new Thread(myThread);
        Thread t2 = new Thread(myThread);
        Thread t3 = new Thread(myThread);
        Thread t4 = new Thread(myThread);
        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");
        t4.setName("窗口4");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }


}

class MyThread1 implements Runnable {
    //实现类的方式来创建多线程更适用于有共享数据的
    private int tickte = 100;


    public void run() {

        while (true) {
            if(tickte>0)
            show();
            else break;
            }
        }

        public synchronized void show(){

                if (tickte > 0) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":" + tickte);
                    tickte--;
                }
        }


}